<?php
class DaneKontaktowe extends Controller
{
	function DaneKontaktowe()
	{
		parent::Controller();
		$this->load->helper('url');
        $this->load->helper('form');
        $this->load->helper('email');
	}
	
	
	
	function infooopiekunach($id)
	{
		
		if(NULL ==  getenv('HTTP_REFERER'))
		{
			echo "NASTĄPIŁA PRÓBA WYŁUDZENIA INFORMACJI !";
		}
		else
		{
			$query = "SELECT p.firstname AS imie, p.secondname AS nazwisko, p.telephone AS telefon, p.email AS email, p.address AS adres FROM parentts p, students s, parentts_students ps 
					WHERE s.id = ps.student_id AND ps.parentt_id = p.id AND s.id =" . $id;
			$opiekunowie = $this->db->query($query);
		
			$query = "SELECT * FROM students WHERE id = " .$id;
			$uczen = $this->db->query($query);
		
			$tablica = array('opiekunowie' => $opiekunowie, 'uczen' => $uczen);
        
			$this->load->view("danekontaktowe_uczen_view", $tablica);
		}
		
	}
	
	
	
	function edytujnauczyciela()
	{
		
		$imie  = $this->input->post('imie');
		$nazwisko  = $this->input->post('nazwisko');
		
		$query = "SELECT * FROM teachers WHERE firstname = '" . $imie . "' AND secondname = '" . $nazwisko . "'";
		$nauczyciel = $this->db->query($query);
		
		foreach ($nauczyciel->result() as $row)
    	{
    		$imie = $row->firstname; 
    		$nazwisko = $row->secondname;
    		$telefon = $row->telephone;
    		$email = $row->email;
        }
		
		
		$tablica = array('imie' => $imie, 'nazwisko' => $nazwisko, 'telefon' => $telefon, 'email' => $email);
		$this->load->view("dane_kontaktowe_nauczyciel_zmien_view", $tablica);
		
	}
	
	
	
	function edytujnauczycielazmien()
	{
		
		$this->load->library('form_validation');
		
       
		$this->form_validation->set_rules('telefon', 'telefon', 'trim|required|numeric');
		$this->form_validation->set_rules('email', 'email', 'required|valid_email');
		
		if ($this->form_validation->run() == FALSE)
		{
			
			$nazwisko =  $this->input->post('nazwisko');
    		$telefon = $this->input->post('telefon');
    		$email = $this->input->post('email');
        	$imie = $this->input->post('imie'); 
        	
			$tablica = array('imie' => $imie, 'nazwisko' => $nazwisko, 'telefon' => $telefon, 'email' => $email);
			$this->load->view("dane_kontaktowe_nauczyciel_zmien_view", $tablica);
		}
		else
		{
		
			$query = "UPDATE teachers SET telephone = '" . $this->input->post('telefon') . "', email = '" . $this->input->post('email') . "'
				 WHERE firstname ='" . $this->input->post('imie') . "' AND secondname ='" . $this->input->post('nazwisko') . "'";
		
			$query = $this->db->query($query);
		
			$query = "SELECT * FROM teachers WHERE firstname = '" . $this->input->post('imie') . "' AND secondname = '" . $this->input->post('nazwisko') . "'";
			$nauczyciel = $this->db->query($query);
		
			foreach ($nauczyciel->result() as $row)
    		{
    			$imie = $row->firstname; 
    			$nazwisko = $row->secondname;
    			$telefon = $row->telephone;
    			$email = $row->email;
        	}
			
			$tablica = array('imie' => $imie, 'nazwisko' => $nazwisko, 'telefon' => $telefon, 'email' => $email);
			$this->load->view("dane_kontaktowe_nauczyciel_zmien_view", $tablica);
		
		}
			
	}
	
	
	
	function edytujrodzica()
	{
		
		$imie  = $this->input->post('imie');
		$nazwisko  = $this->input->post('nazwisko');
		
		$query = "SELECT * FROM parentts WHERE firstname = '" . $imie . "' AND secondname = '" . $nazwisko . "'";
		$nauczyciel = $this->db->query($query);
		
		foreach ($nauczyciel->result() as $row)
    	{
    		$imie = $row->firstname; 
    		$nazwisko = $row->secondname;
    		$telefon = $row->telephone;
    		$email = $row->email;
    		$adres = $row->address;
        }
		
		
		
        
        $tablica = array('imie' => $imie, 'nazwisko' => $nazwisko, 'telefon' => $telefon, 'email' => $email, 'adres' => $adres);
		$this->load->view("dane_kontaktowe_rodzic_zmien_view", $tablica);
		
	}
	
	
	function edytujrodzicazmien()
	{		
				
        $this->load->library('form_validation');
		
       
		$this->form_validation->set_rules('telefon', 'telefon', 'trim|required|numeric');
		$this->form_validation->set_rules('email', 'email', 'trim|required|valid_email');
		$this->form_validation->set_rules('adres', 'adres', 'required');
        
        if ($this->form_validation->run() == FALSE)
		{
			$imie = $this->input->post('imie');
			$nazwisko = $this->input->post('nazwisko');
			$telefon = $this->input->post('telefon');
			$email = $this->input->post('email');
			$adres = $this->input->post('adres');
			
			$tablica = array('imie' => $imie, 'nazwisko' => $nazwisko, 'telefon' => $telefon, 'email' => $email, 'adres' => $adres);
			$this->load->view("dane_kontaktowe_rodzic_zmien_view", $tablica);
		}
		else
		{
		
			$imie = $this->input->post('imie');
			$nazwisko = $this->input->post('nazwisko');
			
			$query = "UPDATE parentts SET telephone='" . $this->input->post('telefon') . "', email = '" . $this->input->post('email') ." ', 
					address ='" . $this->input->post('adres') . " ' WHERE firstname = '" . $imie . "' AND secondname = '" . $nazwisko . "'";
			$query = $this->db->query($query);
		
			$query = "SELECT * FROM parentts WHERE firstname = '" . $imie . "' AND secondname = '" . $nazwisko . "'";
			$nauczyciel = $this->db->query($query);
		
			foreach ($nauczyciel->result() as $row)
    		{
    			$imie = $row->firstname; 
    			$nazwisko = $row->secondname;
    			$telefon = $row->telephone;
    			$email = $row->email;
    			$adres = $row->address;
        	}
        	
			$tablica = array('imie' => $imie, 'nazwisko' => $nazwisko, 'telefon' => $telefon, 'email' => $email, 'adres' => $adres);
			$this->load->view("dane_kontaktowe_rodzic_zmien_view", $tablica);
		
		}     
		
	}
	
	
	
	function infoonauczycielach($id)
	{

		if(NULL ==  getenv('HTTP_REFERER'))
		{
			echo "NASTĄPIŁA PRÓBA WYŁUDZENIA INFORMACJI !";
		}
		else
		{
		
			if($id == "brak")
			{
				$this->load->view("dane_kontaktowe_nauczyciel_brak_view");
			}
			else
			{
				$query = "SELECT firstname AS imie, secondname AS nazwisko, telephone AS telefon, email AS email FROM teachers WHERE id =" . $id;
				$query = $this->db->query($query);
		
				foreach ($query->result() as $row)
    			{
    				$imie = $row->imie; 
    				$nazwisko = $row->nazwisko;
    				$telefon = $row->telefon;
    				$email = $row->email;
        		}
		
        		$tablica = array('imie' => $imie, 'nazwisko' => $nazwisko, 'telefon' => $telefon, 'email' => $email);
        		$this->load->view("dane_kontaktowe_nauczyciel_view", $tablica);
    		}
		}
	}
	
	
	
	function edytujucznia()
	{
			
		$id = $this->input->post('id');
		
		$query = "SELECT c.name AS klasa, s.firstname AS imie, s.secondname AS nazwisko, s.birth AS urodziny, s.email AS email FROM students s, classes c 
				WHERE s.classe_id = c.id AND s.id =" . $id;
		$uczen = $this->db->query($query);
		
		foreach ($uczen->result() as $row)
    	{
    		$imie = $row->imie; 
    		$nazwisko = $row->nazwisko;
    		$klasa = $row->klasa;
    		$email = $row->email;
    		$urodziny = $row->urodziny;
        }
		
		$tablica = array('imie' => $imie, 'nazwisko' => $nazwisko, 'klasa' => $klasa, 'email' => $email, 'urodziny' => $urodziny, 'id' => $id );
        $this->load->view("dane_kontaktowe_student_zmien_view", $tablica);
	}
	
	
	
	function edytujstudentazmien()
	{
		
		$email  = $this->input->post('email');
		$id = $this->input->post('id');
		
		
		$this->load->library('form_validation');
		
		$this->form_validation->set_rules('email', 'e-mail', 'valid_email|required');
		
		if ($this->form_validation->run() == FALSE)
		{
			$query = "SELECT c.name AS klasa, s.firstname AS imie, s.secondname AS nazwisko, s.birth AS urodziny, s.email AS email FROM students s, classes c 
					WHERE s.classe_id = c.id AND s.id =" . $id;
			$uczen = $this->db->query($query);
		
			foreach ($uczen->result() as $row)
    		{
    			$imie = $row->imie; 
    			$nazwisko = $row->nazwisko;
    			$klasa = $row->klasa;
    			$urodziny = $row->urodziny;
        	}
			
			$tablica = array('imie' => $imie, 'nazwisko' => $nazwisko, 'klasa' => $klasa, 'email' => $email, 'urodziny' => $urodziny, 'id' => $id );
        	$this->load->view("dane_kontaktowe_student_zmien_view", $tablica);
		}
		else
		{
		
			$email  = $this->input->post('email');
			$id = $this->input->post('id');
		
			$query = "UPDATE students SET email ='" . $email . "' WHERE id =" . $id;
			$query = $this->db->query($query);
		
			$query = "SELECT c.name AS klasa, s.firstname AS imie, s.secondname AS nazwisko, s.birth AS urodziny, s.email AS email FROM students s, classes c 
					WHERE s.classe_id = c.id AND s.id =" . $id;
			$uczen = $this->db->query($query);
		
			foreach ($uczen->result() as $row)
    		{
    			$imie = $row->imie; 
    			$nazwisko = $row->nazwisko;
    			$klasa = $row->klasa;
    			$email = $row->email;
    			$urodziny = $row->urodziny;
        	}
			
			$tablica = array('imie' => $imie, 'nazwisko' => $nazwisko, 'klasa' => $klasa, 'email' => $email, 'urodziny' => $urodziny, 'id' => $id );
        	$this->load->view("dane_kontaktowe_student_zmien_view", $tablica);
		
		}	
	}
}
?>